//
//  ViewController.m
//  LocalNPushNotificaionDemo
//
//  Created by holyen on 12-10-11.
//  Copyright (c) 2012年 holyen. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addLocalNotification
{
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:20];//20秒后本地推送
    UILocalNotification *noti = [[[UILocalNotification alloc] init] autorelease];
    if (noti)
    {
        //设置推送时间:
        noti.fireDate = date;
        noti.timeZone = [NSTimeZone defaultTimeZone];//时区
        noti.repeatInterval = NSHourCalendarUnit;//设置重复间隔
        noti.soundName = UILocalNotificationDefaultSoundName;
        noti.alertBody = @"Push Demo Test By Holyen";
        noti.applicationIconBadgeNumber = 1;//显示图标上的数字
        NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
        noti.userInfo = infoDic;
        UIApplication *app = [UIApplication sharedApplication];
        [app scheduleLocalNotification:noti];
    }
}

- (void)cancelLocalNotification
{
    UIApplication *app = [UIApplication sharedApplication];
    NSArray *localArr = [app scheduledLocalNotifications];
    if (localArr)
    {
        for (UILocalNotification *noti in localArr)
        {
            NSDictionary *dict = noti.userInfo;
            if (dict)
            {
                NSString *inKey = [dict objectForKey:@"key"];
                if ([inKey isEqualToString:@"name"] && noti)
                {
                    [app cancelLocalNotification:noti];
                    break;
                }
            }
        }
    }
}

- (IBAction)turnonNoti:(id)sender
{
    [self addLocalNotification];
}

- (IBAction)turnoffNoti:(id)sender
{
    [self cancelLocalNotification];
}
@end
